//
// Description: 851. spfa求最短路
// Created by Loading on 2022/5/21.
//

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 1e5 + 10;
constexpr int INF = 0x3f3f3f3f;

// 邻接表
int h[N], e[N], ne[N], w[N], idx;
// 存储 1 号点到每个点的最短距离
int dist[N];
// 存储每个点是否在队列中
bool st[N];

int n;

// 增加一条由a指向b，且权重为c的边
void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void spfa() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    queue<int> q;
    q.emplace(1);
    st[1] = true;

    while (!q.empty()) {
        int t = q.front();
        q.pop();

        // 弹出队列，标识位置为 false
        st[t] = false;
        for (int i = h[t]; i != -1; i = ne[i]) {
            int j = e[i];
            if (dist[j] > dist[t] + w[i]) {
                dist[j] = dist[t] + w[i];
                // 不在队列中才加入队列
                if (!st[j]) {
                    q.emplace(j);
                    st[j] = true;
                }
            }
        }
    }

    if (dist[n] == INF) {
        puts("impossible");
    } else {
        cout << dist[n] << endl;
    }
}

int main() {
    int m;
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; ++i) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        add(x, y, z);
    }

    spfa();

    return 0;
}
